package com.ruoyi.web.controller.yf;

import java.util.List;

import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.yf.tool.barcode.Rules;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yf.domain.YfTag;
import com.ruoyi.yf.service.IYfTagService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;


/**
 * 条形码Controller
 *
 * @author XiaoYi...
 * @date 2020-03-17
 * 6b384c08-6245-4b23-8c35-6cf0169f3b82
 */
@Controller
@RequestMapping("/yf/tag")
@Api(tags = "条形码信息管理", value = "条形码信息管理")
public class YfTagController extends BaseController {
    private String prefix = "yf/tag";

    @Autowired
    private IYfTagService yfTagService;

    //卫生员查询
    @RequiresPermissions("yf:tag:sanitation:view")
    @GetMapping("/sanitation")
    public String sanitationTagManger() {
        return prefix + "/sanitation";
    }


    //暂存员查询
    @RequiresPermissions("yf:tag:persistence:view")
    @GetMapping("/persistence")
    public String persistenceTagManger() {
        return prefix + "/persistence";
    }


    /**
     * @param
     * @return
     * @throws
     * @msg: 验证条形码正确性
     * @Author: XiaoYi...
     * @Date : 2020-03-19 10:40
     * b1597f39-f08c-4374-97ab-dc7e9a118b8d
     */
    @PostMapping("/erify")
    @ResponseBody
    @Log(title = "条形码规则验证")
    @ApiOperation("条形码规则验证")
    @ApiImplicitParam(name = "code", value = "医废条形码", required = true, paramType = "path", dataType = "String")
    public AjaxResult erifyCode(@RequestParam("code") String code) {
        //规则校验
        switch (code.length()) {
            case 20: {//j
                if (!code.substring(0, 2).equals("yl") && !Rules.codeCheck(code.substring(2, code.length())))
                    return error("条形码规则错误");
                //数据库 重复校验
                if (yfTagService.selectYfTagById(code) == null)
                    return successful();
                return error("条形码数据库校验失败");
            }
            case 18: {//旧条码规则
                if (!Rules.codeCheck(code))
                    return error("条形码规则错误");
                //数据库 重复校验
                if (yfTagService.selectYfTagById(code) == null)
                    return successful();
                return error("条形码数据库校验失败");
            }
            case 22: {//新条码规则
                if (!Rules.codeCheckLong(code))
                    return error("条形码规则错误");
                //数据库 重复校验
                if (yfTagService.selectYfTagById(code) == null)
                    return successful();
                return error("条形码数据库校验失败");
            }
            default: {
                return error("条码长度校验错误");
            }
        }
    }

    @PostMapping("add/codeid")
    @ResponseBody
    public AjaxResult getCodeid(@RequestParam("length") int length) {
        String barcode = null;
        if (length == 18)
            barcode = new Rules().barcode();
        else if (length == 20) {
            barcode = new Rules().longBarcode();
        }
        return AjaxResult.successful("获取成功", barcode);
    }

    //电子联单
    //6f750edb-3842-41ea-8af9-540a8562aea9
    @RequiresPermissions("yf:tag:view")
    @GetMapping()
    public String tag() {
        return prefix + "/tag";
    }

    /**
     * acd6507b-694d-4a0e-a44b-fc2cef2ac0d5
     * 查询条形码列表
     */
    @RequiresPermissions("yf:tag:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YfTag yfTag) {
        startPage();
        List<YfTag> list = yfTagService.selectYfTagList(yfTag);
        return getDataTable(list);
    }

    /**
     * 查询条形码列表
     * 2da8ea7c-050c-421f-b7c7-29c2a1e7955b
     */
    @RequiresPermissions("yf:tag:list")
    @PostMapping("/listOnly")
    @ResponseBody
    public TableDataInfo listOnly(YfTag yfTag) {
        startPage();
        List<YfTag> list = yfTagService.selectYfTagListOnly(yfTag);
        return getDataTable(list);
    }


    /**
     * 导出条形码列表
     * 91d985bb-26d1-448d-968e-558d2c0a275a
     */
    @RequiresPermissions("yf:tag:export")
    @Log(title = "条形码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YfTag yfTag) {
        List<YfTag> list = yfTagService.selectYfTagList(yfTag);
        ExcelUtil<YfTag> util = new ExcelUtil<YfTag>(YfTag.class);
        return util.exportExcel(list, "tag");
    }

    /**
     * 新增条形码
     * 561e0195-f195-4466-9bf5-28e3366f1929
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


    /**
     * 新增条形码
     * 498a7494-2be3-40eb-a516-699f1e891fee
     */
    @GetMapping("/add_batch")
    public String addBatch() {
        return prefix + "/add_batch";
    }

    /**
     * 新增保存条形码
     * 9a488f1e-e1c9-4ab7-b12c-4f4270a52e70
     */
    @RequiresPermissions("yf:tag:add")
    @Log(title = "条形码", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YfTag yfTag) {
        SysUser sysUser = ShiroUtils.getSysUser();
        yfTag.setCreateId(sysUser.getUserId());
        yfTag.setCreateBy(sysUser.getLoginName());
        return toAjax(yfTagService.insertYfTag(yfTag));
    }

    /**
     * 修改条形码
     * 9ade4329-60fb-415e-907b-d57bbeeca39c
     */
    @GetMapping("/edit/{codeid}")
    public String edit(@PathVariable("codeid") String codeid, ModelMap mmap) {
        YfTag yfTag = yfTagService.selectYfTagById(codeid);
        mmap.put("yfTag", yfTag);
        return prefix + "/edit";
    }

    /**
     * 修改报数条形码
     * b0508e85-1f90-4b38-938a-544b8b4b7053
     */
    @GetMapping("/edit_batch/{codeid}")
    public String editBatch(@PathVariable("codeid") String codeid, ModelMap mmap) {
        YfTag yfTag = yfTagService.selectYfTagById(codeid);
        mmap.put("yfTag", yfTag);

        return prefix + "/edit_batch";
    }

    /**
     * 修改保存条形码
     * e95d22f2-0914-4d96-8566-54fcf834f207
     */
    @RequiresPermissions(value={"yf:tag:edit", "yf:tag:edits"}, logical = Logical.OR)
    @Log(title = "条形码", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YfTag yfTag) {
        SysUser sysUser = ShiroUtils.getSysUser();
        yfTag.setUpdateId(sysUser.getUserId());
        yfTag.setUpdateBy(sysUser.getLoginName());
        return toAjax(yfTagService.updateYfTag(yfTag));
    }

    /**
     * 删除条形码
     * 3c1e556d-dbaa-45cd-801f-c1c582a2af5f
     */
    @RequiresPermissions("yf:tag:remove")
    @Log(title = "条形码", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(yfTagService.deleteYfTagByIds(ids));
    }

    /**
     * 假删除条形码 标记删除条形码
     *
     * @param ids
     * @return
     * 199d00f5-edc7-42cc-a02c-77d1bb9d7bdd
     */
    @RequiresPermissions("yf:tag:Uremove")
    @Log(title = "条形码", businessType = BusinessType.FAKE_DELETE)
    @PostMapping("/Uremove")
    @ResponseBody
    public AjaxResult uRremove(String ids) {
        return toAjax(yfTagService.updateRremoveAndRecoveryTagByIds(ids, "2"));
    }


    /**
     * 恢复假删除条形码 恢复标记删除条形码
     *
     * @param ids
     * @return
     * 886a5cef-c4c4-4d19-ba3f-7f24f0fe26b0
     */
    @RequiresPermissions("yf:tag:URecovery")
    @Log(title = "条形码", businessType = BusinessType.Update_Recovery)
    @PostMapping("/URecovery")
    @ResponseBody
    public AjaxResult URecovery(String ids) {
        return toAjax(yfTagService.updateRremoveAndRecoveryTagByIds(ids, "0"));
    }


    ///////////////////////////接口/////////////////

    //插入医废 -录入\采集
    public AjaxResult insertTag() {
        return null;
    }


    //更新医废\入库
    public AjaxResult updateTagInto() {
        return null;
    }

    //更新医废\出库
    //c41acf44-e622-4993-abc4-ce27266648c1
    @ApiModelProperty()
    @ApiOperation("医废出库请求")
    @RequiresPermissions("yf:tag:updateTagOut")
    @ResponseBody
    @RequestMapping(value = "/updateTagOut", method = RequestMethod.POST)
    public AjaxResult updateTagOut(YfTag yfTag) {
        return null;
    }


    ////////报表数据查询////////
    //根据查询查询医废类型的数量、重量；
    //fb067f2c-7d2f-42ca-a5c3-4b1ff4821c37
    public TableDataInfo report_01(Long Hospitalid) {
//        Map<String, TreeMap<String, Object>> reports = yfTagService.report_01(Hospitalid);
//        System.out.println(reports);
        return null;
    }

}
